package domain;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

public class Grid {
	
	private final String DAYS_EMPTY = "No one day listed in Grid.";
	
	private String name;
	private Calendar creationDate;
	private List<Day> days;
	private List<Turn> turns;
	private List<Schedule> schedules;
	
	public Grid(String name) {
		this.setName(name);
		this.creationDate = new GregorianCalendar();
		this.setDays(new ArrayList<Day>());
		this.setTurns(new ArrayList<Turn>());
		this.setSchedules(new ArrayList<Schedule>());
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Calendar getCreationDate() {
		return this.creationDate;
	}
	
	public List<Turn> getTurns() {
		return turns;
	}

	public void setTurns(List<Turn> turns) {
		this.turns = turns;
	}
	
	public void addTurn(Turn turn) {
		this.turns.add(turn);
	}
	
	public void removeTurn(Turn turn) {
		this.turns.remove(turn);
	}
	
	public List<Day> getDays() {
		return this.days;
	}
	
	public void setDays(List<Day> days) {
		this.days = days;
	}
	
	public void addDay(Day day) {
		this.days.add(day);
	}

	public void removeDay(Day day) {
		this.days.remove(day);
	}
	
	public List<Schedule> getSchedules() {
		return schedules;
	}
	
	public void setSchedules(List<Schedule> schedules) {
		this.schedules = schedules;
	}
	
	public void addSchedule(Schedule s) {
		this.schedules.add(s);
	}
	
	public void removeSchedule(Schedule s) {
		this.schedules.remove(s);
	}
	
	public void removeAllSchedules() {
		this.schedules = new ArrayList<Schedule>();
	}
	
	public List<Schedule> getSchedulesByDay(Day day) {
		List<Schedule> filteredSchedules = new ArrayList<Schedule>();
		Iterator<Schedule> it = this.getSchedules().iterator();
		while (it.hasNext()) {
			Schedule s = it.next();
			if (s.getDay().equals(day)) {
				filteredSchedules.add(s);
			}
		}
		return filteredSchedules;
	}
	
	public List<Schedule> getSchedulesByDuration(PTime duration) {
		List<Schedule> filteredSchedules = new ArrayList<Schedule>();
		Iterator<Schedule> it = this.getSchedules().iterator();
		while (it.hasNext()) {
			Schedule s = it.next();
			if (s.getDuration().equals(duration)) {
				filteredSchedules.add(s);
			}
		}
		return filteredSchedules;
	} 
	
	public List<PTime> getExistentSchedulesDurations() {
		List<PTime> durations = new ArrayList<PTime>();
		if (this.getDays() == null || this.getDays().isEmpty()) {
			throw new IllegalStateException(DAYS_EMPTY);
		}
		Iterator<Schedule> it = this.getSchedulesByDay(this.getDays().get(0)).iterator();
		while (it.hasNext()) {
			Schedule s = it.next();
			if (!durations.contains(s.getDuration())) {
				durations.add(s.getDuration());
			}
		}
		return durations;
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == null || this.getClass() != o.getClass()) {
			return false;
		}
		if (o == this) {
			return true;
		}
		Grid g = (Grid) o;
		return this.getCreationDate().equals(g.getCreationDate());
	}
	
}
